import { observable, action, toJS } from 'mobx';
import * as sendBatchImportSmsApi from '../../apis/sendBatchImportSms';
import * as sendSmsApi from '../../apis/sendSms';
import * as smsTempApi from '../../apis/smsTemp';
import { WeaLocaleProvider } from 'ecCom';
import { message } from 'antd';
import moment from 'moment';

const getLabel = WeaLocaleProvider.getLabel;

export default class SmsBatchImportStore {
    @observable visibleResult = false;
    @observable importResultColumns = [{
        title: getLabel(24960, '提示信息'),
        width: '100%',
        dataIndex: 'message',
        render: (text, record, index) => <span style={{ color: '#FF0000' }}>{text}</span>,
    }];
    @observable importResultDatas = []
    @observable loading = false;
    @observable showFlag = false;
    @observable msg = '';
    @observable hasRight = '';
    @observable showReply = false;
    @observable groups = [];
    @observable msgCount = 0;

    @action setLoading = (bol) => {
        this.loading = bol;
    }
    @action setGroup = (data) => {
        this.groups = data;
    }

    @action
    getBaseData(params) {
        params = { ...params, ...this.urlParams };
        sendSmsApi.getBaseData(params).then((data) => {
            const { canSend, isGov, showReply, sendHrm, sendCrm, msg, showCusSign, requestid } = data;
            if (canSend) {
                this.hasRight = '1';
                this.isGov = isGov;
                this.showReply = showReply;
                this.sendHrm = sendHrm;
                this.sendCrm = sendCrm;
                let crmid = [];
                if (sendCrm) {
                    for (let index in sendCrm) {
                        if (sendCrm[index].id) {
                            crmid.push(sendCrm[index].id);
                        }
                    }
                }
                this.crmIds = crmid.toString();
                this.showCusSign = showCusSign;
                this.requestid = requestid;
                if (msg) {
                    this.msg = msg;
                    this.msgCount = msg.length;
                } else {
                    this.msgCount = 0;
                }
            } else {
                this.hasRight = '0';
            }
        });
    }

    @action
    send(params) {
        return sendSmsApi.send(params);
    }

    clearStatus() {
        this.showFlag = false;
        this.msg = '';
        this.groups = [];
        this.msgCount = 0;
    }

    @action
    setMsg(msg) {
        this.msg = msg;
        this.msgCount = msg.length;
    }

    @action
    getOptions(params) {
        smsTempApi.getOptions(params).then((data) => {
            this.groups = data;
        });
    }

    @action
    openTempList() {
        this.showFlag = true;
    }

    @action
    closeTempList() {
        this.showFlag = false;
    }


    @action
    createSmsBatch = (params, callback) => {
        sendBatchImportSmsApi.createSmsBatch(params).then((result) => {
            if (result.status && !result.errMsg) {
                if (params.regularChk == 1) {
                    message.success(getLabel(521764, '已保存短信至"定时发送短信列表"'));
                } else {
                    message.success(getLabel(521765, '发送成功'));
                }
                if (callback) {
                    callback();
                }
                this.setMsg('');
            } else {
                message.warning(result.errMsg);
            }
            this.setLoading(false);
        });
    }

    @action
    smsBatchCheck = (params, callback, successCallBack) => {
        sendBatchImportSmsApi.smsBatchCheck(params).then((result) => {
            if (result.status && !result.errMsg) {
                const hasRepeat = result.hasRepeat;
                const hasEmptyMsg = result.hasEmptyMsg;
                if (hasRepeat || hasEmptyMsg) { // txt
                    this.importResultDatas = result.errorInfo;
                    this.visibleResult = true;
                    this.setLoading(false);
                } else {
                    this.createSmsBatch(params, successCallBack);
                }
            } else {
                this.setLoading(false);
                message.warning(result.errMsg);
            }
        });
    }

    downloadTxt = () => {
        this.smsRegularExportTxtUrl = encodeURI(`${'/api/sms/batch/downloadTxt?' + '_t='}${new Date().getTime()}`);
        if (!window.location.origin) {
            window.location.origin = `${window.location.protocol}//${window.location.hostname}${window.location.port ? `:${window.location.port}` : ''}`;
        }
        window.location.href = `${location.origin}${this.smsRegularExportTxtUrl}`;
    }
}
